package com.cebbank.sz.cebtemplate.config;

import org.springframework.beans.BeansException;
import org.springframework.core.io.support.PropertiesLoaderUtils;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * @author sucz
 * @create 2019/12/5 12:16
 */
public class PropertiesListenerConfig {

    private static Map<String, String> propertiesMap = new HashMap<>(16);

    private static void processProperties(Properties properties) throws BeansException {
        propertiesMap = new HashMap<>(16);
        for (Object key : properties.keySet()){
            String keyStr = key.toString();
            try {
                propertiesMap.put(keyStr, new String(properties.getProperty(keyStr).getBytes("ISO-8859-1")));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static void loadAllProperties(String propertyFileName) {
        try {
            Properties properties = PropertiesLoaderUtils.loadAllProperties(propertyFileName);
            processProperties(properties);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static String getProperty(String name) {
        return propertiesMap.get(name);
    }

    public static Map<String, String> getAllProperty(String name) {
        return propertiesMap;
    }

}
